package com.google.code.hostsman.db;

import android.provider.BaseColumns;

public final class DatabaseContract {

	// constants to work with the database
	public static final String PRAGMA_FOREIGN_KEYS_ON="PRAGMA foreign_keys=ON;";
	public static final int BOOLEAN_TRUE = 1;
	public static final int BOOLEAN_FALSE = 0;

	// string constants for TABLE_CREATE
	private static final String CREATE_TABLE = "CREATE TABLE ";
	private static final String DROP_TABLE_IF_EXISTS = "DROP TABLE IF EXISTS ";
	private static final String TEXT_TYPE = " TEXT";
	private static final String INTEGER_TYPE = " INTEGER";
	private static final String PRIMARY_KEY_AUTO = " PRIMARY KEY AUTOINCREMENT";
	private static final String NOT_NULL = " NOT NULL";
	private static final String FOREIGN_KEY = "FOREIGN KEY";
	private static final String REFERENCES = "REFERENCES ";
	private static final String ON_DELETE_CASCADE = " ON DELETE CASCADE";
	private static final String COMMA_SEP = ",";
	private static final String OPEN_BRACKET = "(";
	private static final String CLOSING_BRACKET = ")";
	private static final String END = ";";

	/**
	 * Current version of the database.
	 */
	public static final int DATABASE_VERSION = 1;

	/**
	 * Name of the database.
	 */
	public static final String DATABASE_NAME = "hosts.db";

	private DatabaseContract() {
	}

	public static abstract class DomainIPPairEntry implements BaseColumns {
		public static final String TABLE_NAME = "domain_ip_pair";
		public static final String COLUMN_NAME_DOMAIN = "domain";
		public static final String COLUMN_NAME_IP = "ip_address";
		public static final String COLUMN_NAME_GROUP_ID = "group_id";

		public static final String[] ALL_COLUMNS = { _ID, COLUMN_NAME_DOMAIN,
				COLUMN_NAME_IP, COLUMN_NAME_GROUP_ID };

		public static final String TABLE_CREATE = CREATE_TABLE + TABLE_NAME
				+ OPEN_BRACKET + _ID + INTEGER_TYPE + PRIMARY_KEY_AUTO
				+ COMMA_SEP + COLUMN_NAME_DOMAIN + TEXT_TYPE + NOT_NULL
				+ COMMA_SEP + COLUMN_NAME_IP + TEXT_TYPE + NOT_NULL + COMMA_SEP
				+ COLUMN_NAME_GROUP_ID + INTEGER_TYPE + NOT_NULL + COMMA_SEP
				+ FOREIGN_KEY + OPEN_BRACKET + COLUMN_NAME_GROUP_ID
				+ CLOSING_BRACKET + REFERENCES
				+ DatabaseContract.GroupEntry.TABLE_NAME + OPEN_BRACKET
				+ DatabaseContract.GroupEntry._ID + CLOSING_BRACKET
				+ ON_DELETE_CASCADE + CLOSING_BRACKET + END;

		public static final String TABLE_DELETE = DROP_TABLE_IF_EXISTS
				+ TABLE_NAME + END;
	}

	public static abstract class GroupEntry implements BaseColumns {
		public static final String TABLE_NAME = "domain_ip_pair_group";
		public static final String COLUMN_NAME_NAME = "name";
		public static final String COLUMN_NAME_ENABLED = "enabled";

		public static final String[] ALL_COLUMNS = { _ID, COLUMN_NAME_NAME,
				COLUMN_NAME_ENABLED };

		public static final String TABLE_CREATE = CREATE_TABLE + TABLE_NAME
				+ OPEN_BRACKET + _ID + INTEGER_TYPE + PRIMARY_KEY_AUTO
				+ COMMA_SEP + COLUMN_NAME_NAME + TEXT_TYPE + NOT_NULL
				+ COMMA_SEP + COLUMN_NAME_ENABLED + INTEGER_TYPE + NOT_NULL
				+ CLOSING_BRACKET + END;

		public static final String TABLE_DELETE = DROP_TABLE_IF_EXISTS
				+ TABLE_NAME + END;
	}
}